﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.IO;
using System.Drawing.Imaging;

public partial class CMSV2_Controls_FrontPageTop_FrontPageTopInfo : System.Web.UI.UserControl
{
    private Guid Id
    {
        get
        {
            if (!String.IsNullOrEmpty(Request["Id"]))
            {
                return new Guid(Request["Id"]);
            }
            return Guid.Empty;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Id.Equals(Guid.Empty))
            {
                LoadFrontPageTop();

                this.uiBtnSave.Enabled = true;
                this.uiBtnDelete.Enabled = false;
            }
        }
    }

    private void LoadFrontPageTop()
    {
        using (IFrontPageTopService service = ServiceFactory.FrontPageTopService)
        {
            FrontPageTop item = service.Find(this.Id);
            this.uiTbTitle.Text = item.Text;
            this.uiTbText.Text = item.Text;
            this.uiTbLink.Text = item.Link;

            this.uiImgImage.ImageUrl = ResolveUrl("~/FrontBilledTop/" + item.Id.ToString() + ".jpg") + "?uid=" + Guid.NewGuid().ToString();
        }

        this.uiPlhImage.Visible = true;
    }
    protected void uiBtnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            FrontPageTop item;
            if (this.Id.Equals(Guid.Empty))
            {
                item = new FrontPageTop();
            }
            else
            {
                using (IFrontPageTopService service = ServiceFactory.FrontPageTopService)
                {
                    item = service.Find(this.Id);
                }
            }

            item.Title = this.uiTbTitle.Text;
            item.Text = this.uiTbText.Text;
            item.Link = this.uiTbLink.Text;

            using (IFrontPageTopAdminService adminService = ServiceFactory.FrontPageTopAdminService)
            {
                item = adminService.Save(item);
            }

            if (this.Id.Equals(Guid.Empty))
            {
                Response.Redirect("FrontPageTopManager.aspx?Id=" + item.Id.ToString());
            }
        }
    }

    protected void uiBtnDelete_Click(object sender, EventArgs e)
    {
        using (IFrontPageTopAdminService adminService = ServiceFactory.FrontPageTopAdminService)
        {
            adminService.Delete(this.Id);

            string fileName = ResolveUrl("~/FrontBilledTop/" + this.Id.ToString() + ".jpg");
            if (File.Exists(MapPath(fileName)))
            {
                File.Delete(MapPath(fileName));
            }
        }
    }

    protected void uiImageUploader_OnImageTransformationComplte(object sender, System.Drawing.Image transformedImage)
    {
        string fileName = ResolveUrl("~/FrontBilledTop/" + this.Id.ToString() + ".jpg");
        if (File.Exists(MapPath(fileName)))
        {
            File.Delete(MapPath(fileName));
        }
        transformedImage.Save(MapPath(fileName), ImageFormat.Jpeg);
        this.uiImgImage.ImageUrl = ResolveUrl("~/FrontBilledTop/" + this.Id.ToString() + ".jpg") + "?uid=" + Guid.NewGuid().ToString();
    }
}
